﻿using System;

namespace Lr.Templating.FluentMapping
{
    /// <summary>
    /// Implementation of <see cref="INongGenericTemplateMappingBuilder"/>,
    /// which defines a template mapping builder,
    /// a construct that facilitates the mapping fluent api.
    /// </summary>
    internal class NonGenericTemplateMappingBuilder : TemplateMappingBuilderBase, INonGenericTemplateMappingBuilder
    {
        /// <summary>
        /// Constructs a new <see cref="NonGenericTemplateMappingBuilder"/> instance.
        /// </summary>
        /// <param name="template">The template for which we're building a mapping.</param>
        /// <param name="placeholder">The placholder attached to the mapping we're building.</param>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="template"/> is null.</exception>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="placeholder"/> is null, empty or only exists of white spaces.</exception>
        internal NonGenericTemplateMappingBuilder(ITemplate template, string placeholder)
            : base(template, placeholder)
        {
        }

        /// <summary>
        /// Maps a constant string value to the placeholder,
        /// and adds it to the template.
        /// </summary>
        /// <param name="value">A constant string.</param>
        public void To(string value)
        {
            ThrowIf.NullOrEmpty(() => value);

            var mapping = new NonGenericTemplateMapping(this.Placeholder, value);
            this.Template.AddMapping(mapping);
        }

        /// <summary>
        /// Maps a function, that returns a string, which generates a value for the placeholder,
        /// and adds it to the template.
        /// </summary>
        /// <param name="value">A function that returns a string.</param>
        public void To(Func<string> value)
        {
            ThrowIf.Null(() => value);

            var mapping = new NonGenericTemplateMapping(this.Placeholder, value);
            this.Template.AddMapping(mapping);
        }
    }
}
